package com.ruoyi.app.service.impl;

import com.ruoyi.app.domain.OperEquip;
import com.ruoyi.app.domain.OperStaff;
import com.ruoyi.app.domain.Operticket;
import com.ruoyi.app.domain.OperticketStep;
import com.ruoyi.app.service.impl.mapper.OperEquipMapper;
import com.ruoyi.app.service.impl.mapper.OperStaffMapper;
import com.ruoyi.app.service.impl.mapper.OperticketMapper;
import com.ruoyi.app.service.IOperticketService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IOperticketServiceImpl implements IOperticketService {

    @Autowired
    private OperticketMapper operticketMapper;

    @Autowired
    private OperEquipMapper operEquipMapper;


    @Autowired
    private OperStaffMapper operStaffMapper;
    @Override
    public List<Operticket> selectOperticketList(Operticket operticket) {

        List<Operticket> operticketList = operticketMapper.selectOperticketList(operticket);
        if(operticketList.size() > 0){
            for (Operticket operticket1:
            operticketList) {
                OperticketStep oper = new OperticketStep();
                oper.setOperTicketCode(operticket1.getId());
                oper.setTicketType(-1);
                List<OperticketStep> operticketStepList = operticketMapper.selectOperticketStepList(oper);
                if(operticketStepList != null){
                    operticket1.setStepCount(operticketStepList.size());
                }else{
                    operticket1.setStepCount(0);
                }
                if(operticket1.getExecuteStaffId() != 0){
                    OperStaff operStaff = new OperStaff();
                    operStaff.setId((int)operticket1.getExecuteStaffId());
                    List<OperStaff> operStaffList = operStaffMapper.selectOperStaffList(operStaff);
                    if(operStaffList.size() > 0){
                        operticket1.setStaffName(operStaffList.get(0).getStaffName());
                    }else{
                        operticket1.setStaffName("");
                    }

                }else{
                    operticket1.setStaffName("");
                }
                if(operticket1.getStaffId() != 0){
                    OperStaff executeStaff = new OperStaff();
                    executeStaff.setId((int)operticket1.getStaffId());
                    List<OperStaff> executeStaffList = operStaffMapper.selectOperStaffList(executeStaff);
                    if(executeStaffList.size() > 0){
                        operticket1.setExecutor(executeStaffList.get(0).getStaffName());
                    }else{
                        operticket1.setExecutor("");
                    }
                }
            }
        }
        return operticketList;
    }

    @Override
    public List<OperEquip> selectOperEquipList(OperEquip operEquip) {

        List<OperEquip> operEquipList = operEquipMapper.selectOperEquipList(operEquip);
        return operEquipList;
    }

    @Override
    public int updateOperEquipStep(OperticketStep operticketStep) {

        int res = operticketMapper.updateOperEquipStep(operticketStep);
        if(res == 1){
            OperticketStep operticketStep1 = new OperticketStep();
            operticketStep1.setId(operticketStep.getId());

            OperticketStep operticketStepList = operticketMapper.selectOperticketStepById(operticketStep.getId());
            if(operticketStepList != null){
                long tickCode = operticketStepList.getOperTicketCode();

                Operticket operti = new Operticket();
                operti.setId(tickCode);
                Operticket operticket = operticketMapper.selectOperticketByCondition(operti);
                if(operticket != null){
                    if(operticket.getStartTime() == null &&operticket.getState() == 1){
                        operticket.setStartTime(DateUtils.getTime());
                        operticketMapper.updateOperticket(operticket);
                    }
                }

                int counts = operticketMapper.selectOperticketStepCount(tickCode);
                if(counts == 0){
                    Operticket operticket2 = new Operticket();
                    operticket2.setId(tickCode);
                    operticket2.setState(2);
                    operticket2.setEndTime(DateUtils.getTime());
                    updateOperticket(operticket2);
                }
            }
        }
        return res;
    }

    @Override
    public List<OperStaff> selectOperStaffList(OperStaff operStaff) {
        List<OperStaff> operEquipList = operStaffMapper.selectOperStaffList(operStaff);
        return operEquipList;
    }

    @Override
    public List<OperticketStep> selectOperticketStepList(OperticketStep operticketStep) {
        List<OperticketStep> operticketStepList = operticketMapper.selectOperticketStepList(operticketStep);
        return operticketStepList;
    }

    @Override
    public OperticketStep selectOperticketStepById(Long id) {
        return operticketMapper.selectOperticketStepById(id);
    }

    @Override
    public int updateOperticket(Operticket operticket) {

        int res = operticketMapper.updateOperticket(operticket);
//        if(res == 1){
//            Operticket operticket2 = new Operticket();
//            operticket2.setId(operticket.getId());
//            operticket2.setState(0);
//            updateOperticket(operticket2);
//        }
        return res;
    }
}
